package cn.bob.org.service.impl;

import cn.bob.basic.query.DeptQuery;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Dept;
import cn.bob.org.domain.PowerId;
import cn.bob.org.mapper.DeptMapper;
import cn.bob.org.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    @Transactional
    public void addDeept(Dept dept) {
        dept.setCreatetime(new Date());
        deptMapper.addDeept(dept);
    }

    @Override
    @Transactional
    public void delDept(Long id) {
        deptMapper.delDept(id);
    }

    @Override
    @Transactional
    public void updateDept(Dept dept) {
        dept.setCreatetime(new Date());
        deptMapper.updateDept(dept);
    }

    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public Dept findByDeptId(Long id) {
        return deptMapper.findByDeptId(id);
    }

    @Override
    public Integer queryCount(DeptQuery deptQuery) {
        return deptMapper.queryCount(deptQuery);
    }

    @Override
    public PageList<Dept> queeryByPage(DeptQuery deptQuery) {
        // 查询条数
        Integer totals = deptMapper.queryCount(deptQuery);
        if (null ==totals || totals==0){
            return new PageList<>(new ArrayList<>(),0);
        }
        // 查询数据
        List<Dept> deptPageList = deptMapper.queeryByPage(deptQuery);
        return  new PageList<>(deptPageList,totals);
    }

    @Override
    public Dept findByUser(String username) {
        return deptMapper.findByUser(username);
    }

}
